package com.he.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.he.pojo.MailBox;
import com.he.pojo.MailBoxExample;
import com.he.pojo.Notice;
import com.he.pojo.User;
import com.he.service.mailbox.MailBoxServiceI;
import com.he.service.notice.NoticeServiceI;

@Controller
@RequestMapping("home")
public class HomeController {
	@Autowired
	private NoticeServiceI  nsi;
	@Autowired 
	MailBoxServiceI boxService;
	@RequestMapping("/show")
	public String showAllNotice(HttpServletRequest request,ModelMap modelMap, String flag) {
		Notice notice = new Notice();
		List<Notice> list = nsi.getNotices(null, notice);
		modelMap.addAttribute("nList", list);
		//主页显示收信箱
		HttpSession session = request.getSession();
		User user=(User)session.getAttribute("user");
		MailBoxExample example=new MailBoxExample();
		example.createCriteria().andReceiveIdEqualTo(user.getId());
		example.setOrderByClause("mail_date desc");
		List<MailBox> mailBoxs = boxService.getPageMailBox(example, new MailBox());
		modelMap.addAttribute("mailBoxs", mailBoxs);
		return "home";
	}
	@RequestMapping("showDetail")
	public String showDetail(ModelMap modelMap, int id) {
		Notice notice = nsi.getNoticeById(id);
		int readNum = notice.getReadNum();
		notice.setReadNum(readNum + 1);
		nsi.update(notice);
		modelMap.addAttribute("notice", notice);
		return "notice_detail";
	}
}
